package app;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;


public class Utils {

    public static void add(AddressList addressList, JdbcTemplate jdbcTemplate) {
        String sql = "insert into list value(?,?,?,?)";
        jdbcTemplate.update(sql, addressList.getName(), addressList.getAddress(), addressList.getPhone(), addressList.getId());
    }

    public static void delete(int id, JdbcTemplate jdbcTemplate) {
        String sql = "delete from list where id = ?";
        jdbcTemplate.update(sql, id);
    }

    public static void update(AddressList addressList, JdbcTemplate jdbcTemplate) {
        String sql = "update list set name = ? , address = ?, phone = ? where id = ?";
        Object[] args = {addressList.getName(), addressList.getAddress(), addressList.getPhone(), addressList.getId()};
        jdbcTemplate.update(sql, args);
    }

    public static List<AddressList> findAll(JdbcTemplate jdbcTemplate) {
        String sql = "select * from list";
        List<AddressList> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<AddressList>(AddressList.class));
        return list;
    }
}
